<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by Free CSS Templates
http://www.freecsstemplates.org
Released for free under a Creative Commons Attribution 2.5 License
-->

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta name="generator" content=
        "HTML Tidy, see www.w3.org" />
        <meta http-equiv="content-type" content=
        "text/html; charset=utf-8" />

        <title>CSC560 Design and Analysis of Real-Time
        Systems</title>
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <link href="../default.css" rel="stylesheet" type=
        "text/css" />
		<link href="../js/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../js/highlight.pack.js"></script>
<script type="text/javascript" src="../js/highlight.js"></script>
<script src="../js/avrasm.js"></script>
<script src="../js/cpp.js"></script>
<script type="text/javascript">
hljs.initHighlightingOnLoad();
</script>
    </head>

    <body>
        <div id="wrapper">
            <div id="header">
                <h1>CSC560</h1>

                <h2>Design and Analysis of Real-Time Systems</h2>

                <ul>
                    <li class="first"><a href="../index.html"
                    accesskey="1" title="">Home</a></li>

                    <li><a href="../project1/index.html" accesskey=
                    "2" title="">Project 1</a></li>

                    <li><a href="index.html" accesskey="3" title=
                    ""><b>Project 2</b></a></li>

                    <li><a href="../project3/index.html" accesskey="4" title="">Project
                    3</a></li>

                    <li><a href="../project4/index.html" accesskey="4" title="">Project
                    4</a></li>

                    <li><a href="../project5/index.html" accesskey="4" title="">Project
                    5</a></li>
                </ul>
            </div>
        </div>

        <div id="content">
            <div id="colOne">
                <h2>Time Measurement</h2>

                <p>We split the code of project 1 into 4 fragments
                to facilitate the measurement of time to execute each task. 
				Each fragment of code corresponds to a task when scheduled 
				with RTOS. For a detailed description of the prior system,
                please refer to <a href="../project1/index.html">Project 1</a>.<br />
                <br />
				
				<h3>Tasks Breakdown </h3>
				
                 1. <i>Joystick task</i> <br>
				 Captures the joystick action as defined in Project 1 and prepares the
                SCI command packet to be transmitted to the remote
                Roomba.
				</li>
				<br />
                <br />
                 2. <i>Radio Transmission task</i> <br>
				  Transmits the
                corresponding command packet.<br />
                <br />
                 3. <i>Radio Receiving task</i><br> Reads and stores the data
                contained in the status packet received from the
                Roomba.<br />
                <br />
                 4. <i>UART task</i><br> Calculates the speed of the
                Roomba and prints it out through UART
                interface.</p>

                <p>The first challenge we faced in this section was
                that the precision given by the Timer_Now()
                function in Timer.c was not precise enough for the
                measurement of certain tasks because it used 5
                milliseconds as the time unit for each tick . For
                example, we noticed that printing to a terminal
                using uart is dependent on the number of characters
                we want to print. The fewer the number of
                characters the faster the task can be executed.
                Indeed, we tried measuring the time it takes to
                print 8 characters using the Timer_Now() function
                in Timer.c and the elapsed time of this task was 0
                milliseconds. We therefore had to obtain better
                precision so that we could calculate the time more
                accurately. We have modified the Timer.c function
                to include a new function, which read TCNT1 and
                obtained the value atomically. This gave us a
                result in unit of 1 millisecond and allowed us to
                get more accurate results. The function implemented
                in Timer.c was similar to the sample_count()
                function found in Timer.c, except that it used
                TCNT1:</p>
<pre>
<code class="cpp">uint16_t TCNT1_Time_Now() {
    uint16_t cnt;
    uint8_t sreg = SREG; // back up the status register
    
    cli();  // clear interrupt bit to disable interrupts
    cnt = TCNT1; // copy TCNT1
    SREG = sreg; // restore the status register
    
    return cnt;
}
</code>
</pre>
                 

                <p>In order to test our new timer function, we
                decided to turn on an LED every second. Since the
                precision of TNCT1 was in microseconds, we needed
                to modify the code we used from project 1 to test
                the Timer_Now() function. The following code shows
                how to make sure TCNT1_Time_Now() is measuring time
                correctly.</p>
<pre>
<code class="cpp">int main(void) {
    uint16_t test_time;
    
    Timer_Init(); //initializing timer
    
    test_time = TCNT1_Time_Now();
    for(;;) {

         /* Every 10 milliseconds, increment c */
         if ((TCNT1_Time_Now() - test_time) &gt;= 10000) {
             c++;
             test_time = TCNT1_Time_Now();
         }
         /* When c reaches 100 which means that 
          * when c reaches 100 x 10 milliseconds = 1 second,
          * turn on LEDs
          */
         if (c==100) {
             PORTD |= _BV(PORTD7);
             _delay_ms(TIME_DELAY);
             PORTD &= 0x00;
             c=0;
         }
    }
}
</code>
</pre>
                <br />
                <br />
                <br />
                 

                <h3>Results of time measurement</h3>

                <h4>Joystick task</h4>

                <p>We ran several experiments on the Joystick task
                and concluded that the maximum time required was 16
				microseconds as shown in the snapshot
                below. Note that the time displayed through uart 
				does not explicitly shows decimal numbers.
				We are therefore rounding the time to 17 
				microseconds.</p>

                <p><img src="../images/measurement_joystick.jpg"
                alt="Joystick Time Measurement Result" /><br />
                </p>

                <h4>Radio Transmission task</h4>
                <br />
                 Similarly, we did several experiments on the radio
                transmission task and concluded that the maximum
                time required was 1.1 milliseconds as shown in the
                snapshot below.<br />
                <br />
                 

                <p><img src=
                "../images/measurement_radio_sending.jpg" alt=
                "Radio Send Time Measurement Result" /><br />
                </p>

                <h4>Radio Receiving task</h4>
                <br />
                 Again, several experiments were conducted on the
                radio receiving task and we concluded that the
                maximum time required was 22 milliseconds as shown
                in the snapshot below.<br />
                <br />
                 

                <p><img src=
                "../images/measurement_radio_receive.jpg" alt=
                "Radio Receiving Time Measurement Result" /><br />
                </p>

                <h4>UART task</h4>
                <br />
                 Lastly, various experiments led us to conclude
                that the maximum time required for UART task was 14 milliseconds
                as shown in the snapshot below.<br />
                <br />
                 

                <p><img src="../images/measurement_uart.jpg" alt=
                "UART Time Measurement Result" /><br />
                </p>
				<br>
                <h4>Overall CPU utilization</h4>
				<br>
<table width="500" border="1">
    <caption>
        CPU Utilization Summary Based on Periodic Scheduling
    </caption>
    <tr>
        <td>
        	Task
        </td>
        <td>
        	Joystick
        </td>
        <td>
        	Radio Tx
        </td>
        <td>
        	Radio Rx
        </td>
        <td>
        	UART
        </td>
        <td>
        	Total
        </td>
    </tr>
    <tr>
        <td>
			Execution time (microseconds)
        </td>
        <td>
        	17
        </td>
        <td>
        	1100
        </td>
        <td>
        	22000
        </td>
        <td>
        	14000
        </td>
        <td>
        	37117
        </td>
    </tr>
    <tr>
        <td>
        	Assigned time slot (microseconds)
        </td>
        <td>
        	5000
        </td>
        <td>
        	10000
        </td>
        <td>
        	25000
        </td>
        <td>
        	20000
        </td>
        <td>
        	60000
        </td>
    </tr>
    <tr>
        <td>
        	CPU Utilization
        </td>
        <td>
        	0.34 %
        </td>
        <td>
        	11.00 %
        </td>
        <td>
        	88.00 %
        </td>
        <td>
        	70.00 %
        </td>
        <td>
        	61.86%
        </td>
    </tr>
    <tr>
        <td>
	               
        </td>
        <td>
        	
        </td>
        <td>
        	
        </td>
        <td>
        	
        </td>
        <td>
        	
        </td>
        <td>
        	
        </td>
    </tr>
    <tr>
        <td>
        	Delay (microseconds)
        </td>
        <td>
        	0
        </td>
        <td>
        	0
        </td>
        <td>
        	20000
        </td>
        <td>
        	0
        </td>
        <td>
        	20000
        </td>
    </tr>
    <tr>
        <td>
        	CPU Utilization ex Delay
        </td>
        <td>
        	0.34 %
        </td>
        <td>
        	11.00 %
        </td>
        <td>
        	8.00 %
        </td>
        <td>
        	70.00 %
        </td>
        <td>
        	28.53 %
        </td>
    </tr>
</table>
				
            </div>

            <div id="colTwo">
                <h3>Project Sections</h3>

                <ul>
                    <li class="first"><a href="part1.html">Time
                    Measurements</a></li>

                    <li><a href="part2.html">Scheduling Tasks with
                    RTOS</a></li>

                    <li>- <a href=
                    "part2sub1.html">Periodic</a></li>

                    <li>- <a href="part2sub2.html">System and Round
                    Robin</a></li>

                    <li>- <a href="part2sub3.html">Event
                    Coordinated</a></li>
					<li>	<a href="doxygen/html/index.html">Doxygen</a></li>
					<li><a href="http://code.google.com/p/wireless-roomba">Google Code</a></li>

                </ul>
            </div>
        </div>

        <div id="footer">
            <p>@ 2009. All Rights Reserved. Design by <a href=
            "http://www.freecsstemplates.org/">Free CSS
            Templates</a>.</p>
        </div>
    </body>
</html>

